package ro.jproject.imc.view;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import ro.jproject.imc.model.DataBaseConnection;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JLabel;
import javax.swing.JTextField;


public class ShowStudentsDataBase extends JFrame {

	private static final long serialVersionUID = 1L;
	private JFrame frame;
	public String id;
	public String firstName;
	public String lastName;
	public String cnp;
	public String status;
	public static DefaultTableModel model;
	public static JTable table;
	private JTextField textFieldCnp;
	private JTextField textFieldLastName;
	private JTextField textFieldFirstName;

	public ShowStudentsDataBase(String id, String firstName, String lastName,
			String cnp, String status) {

		super();

		setTitle("Students DataBase");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setBounds(500, 200, 299, 273);
		setSize(582, 413);
		setVisible(true);

		

		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
		this.cnp = cnp;
		this.status = status;

		model = new DefaultTableModel();
		model.addColumn("Id");
		model.addColumn("First Name");
		model.addColumn("Last Name");
		model.addColumn("CNP");
		model.addColumn("Status");

		

		table = new JTable(model);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				try {
					DataBaseConnection connection = new DataBaseConnection();
					connection.connected();
					int row = table.getSelectedRow();
					String tableClick = (table.getModel().getValueAt(row, 0).toString());
					String sql = "SELECT * FROM students WHERE id = '" + tableClick + "' ";
					DataBaseConnection.statementInsert = DataBaseConnection.connection.prepareStatement(sql);
					ResultSet rs = DataBaseConnection.statementInsert.executeQuery(sql);
					
					if(rs.next()){
						String firstNameCol = rs.getString("firstName");
						textFieldFirstName.setText(firstNameCol);
						String lastNameCol = rs.getString("lastName");
						textFieldLastName.setText(lastNameCol);
						String cnpCol = rs.getString("cnp");
						textFieldCnp.setText(cnpCol);
					}
				}catch(Exception e) {
					JOptionPane.showMessageDialog(null, e);
				}
			}
		});
		
		frame = new JFrame();
		Container container = getContentPane();
		getContentPane().setLayout(null);
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBounds(10, 19, 546, 231);
		container.add(scrollPane);

		

		JButton btnBack = new JButton("Back");
		btnBack.setBounds(327, 333, 183, 31);
		getContentPane().add(btnBack);
		
		JLabel lblFirstName = new JLabel("First Name");
		lblFirstName.setBounds(68, 261, 91, 14);
		getContentPane().add(lblFirstName);
		
		JLabel lblLastName = new JLabel("Last Name");
		lblLastName.setBounds(247, 261, 82, 14);
		getContentPane().add(lblLastName);
		
		JLabel lblCnp = new JLabel("CNP");
		lblCnp.setBounds(441, 261, 46, 14);
		getContentPane().add(lblCnp);
		
		textFieldCnp = new JTextField();
		textFieldCnp.setBounds(385, 286, 142, 20);
		getContentPane().add(textFieldCnp);
		textFieldCnp.setColumns(10);
		
		textFieldLastName = new JTextField();
		textFieldLastName.setBounds(207, 286, 142, 20);
		getContentPane().add(textFieldLastName);
		textFieldLastName.setColumns(10);
		
		textFieldFirstName = new JTextField();
		textFieldFirstName.setBounds(29, 286, 142, 20);
		getContentPane().add(textFieldFirstName);
		textFieldFirstName.setColumns(10);

					
		JButton removeButton = new JButton("Delete registration");
		removeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent E) {
				

				String firstNameCol = textFieldFirstName.getText();
				String lastNameCol = textFieldLastName.getText();
				String cnpCol = textFieldCnp.getText();
				
				DataBaseConnection dataBase = new DataBaseConnection();
				dataBase.connected();
				try {
					
					dataBase.deleteQueryStudents(firstNameCol, lastNameCol, cnpCol);
					
					JOptionPane.showMessageDialog(frame,"Registration deleted!");
					
					dataBase.queryE();
					
					
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				setVisible(false);
			}
		});
		
		removeButton.setBounds(45, 333, 183, 31);
		getContentPane().add(removeButton);
		
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
				Ums frame = new Ums();
				frame.setVisible(true);
			}
		});

		

			

	}
}
